package sealte.util.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 * @author SEALTE
 *
 */
public class MD5 {

	private static String md5s(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			return buf.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 32位加密
	 * @param str_org
	 * @return
	 */
	public static String get32MD5(String str_org){
		return md5s(str_org);
	}
	
	/**
	 * 16位加密
	 * @param str_org
	 * @return
	 */
	public static String get16MD5(String str_org){
		return md5s(str_org).substring(8,24);
	}
}
